package com.example.henry.yindu.fragment;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.example.henry.yindu.R;
import com.example.henry.yindu.activity.FirstPage;
import com.example.henry.yindu.activity.ResourceManager;
import com.example.henry.yindu.adapter.ListViewItemAdapter;
import com.example.henry.yindu.adapter.RollPagerViewAdapter;
import com.example.henry.yindu.myclass.ListViewItem;
import com.example.henry.yindu.myclass.MidBar;
import com.example.henry.yindu.myclass.SVListView;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import android.support.v7.widget.Toolbar;
import android.widget.Toast;

import java.net.Inet4Address;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Henry on 2018/4/6.
 */

public class HomeFragment extends Fragment implements View.OnClickListener{
    private RollPagerView rollPagerView;
    private SVListView listView;
    private List<ListViewItem> list;
    private ScrollView scrollView;
    private Toolbar toolbar;
    private SearchView searchView;
    private AppCompatActivity appCompatActivity;
    private Activity activity;
    private MidBar midBar;
    private LinearLayout l1;
    private LinearLayout l2;
    private LinearLayout l3;
    private LinearLayout l4;


    public HomeFragment () {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View  view = inflater.inflate(R.layout.home_fragment, container, false);
        activity = getActivity();

        rollPagerView = (RollPagerView)view.findViewById(R.id.roll_pager_view);
        rollPagerView.setPlayDelay(2000);
        rollPagerView.setAnimationDurtion(500);
        rollPagerView.setAdapter(new RollPagerViewAdapter());
        rollPagerView.setHintView(new ColorPointHintView(view.getContext(), Color.BLUE, Color.WHITE));
        list = new ArrayList<>();
        for(int i = 0; i < 10;i++) {
            list.add(new ListViewItem("Java精品课程", R.drawable.img1));
        }
        ListViewItemAdapter adapter = new ListViewItemAdapter(view.getContext(), R.layout.list_view_item, list);
        listView = (SVListView)view.findViewById(R.id.list_view);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                ListViewItem item = list.get(position);
                Toast.makeText(getContext(), "xixi", Toast.LENGTH_SHORT).show();
            }
        });
        scrollView = (ScrollView)view.findViewById(R.id.scroll_view);
        scrollView.smoothScrollTo(0, 0);

        l1 = (LinearLayout) view.findViewById(R.id.l1);
        l2 = (LinearLayout) view.findViewById(R.id.l2);
        l3 = (LinearLayout) view.findViewById(R.id.l3);
        l4 = (LinearLayout) view.findViewById(R.id.l4);
        l1.setOnClickListener(this);
        l2.setOnClickListener(this);
        l3.setOnClickListener(this);
        l4.setOnClickListener(this);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceStatus) {
        super.onViewCreated(view, savedInstanceStatus);
        appCompatActivity = (AppCompatActivity)getActivity();
        initToolBar();
    }

    private void initToolBar() {
        toolbar = (Toolbar) appCompatActivity.findViewById(R.id.toolbar1);
        searchView = (SearchView)appCompatActivity.findViewById(R.id.searchView);

        appCompatActivity.setSupportActionBar(toolbar);
        appCompatActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        searchView.setIconifiedByDefault(true);//设置展开后图标的样式,这里只有两种,一种图标在搜索框外,一种在搜索框内
        searchView.onActionViewExpanded();// 写上此句后searchView初始是可以点击输入的状态，如果不写，那么就需要点击下放大镜，才能出现输入框,也就是设置为ToolBar的ActionView，默认展开
        searchView.requestFocus();//输入焦点
        searchView.setSubmitButtonEnabled(true);//添加提交按钮，监听在OnQueryTextListener的onQueryTextSubmit响应
        searchView.setFocusable(true);//将控件设置成可获取焦点状态,默认是无法获取焦点的,只有设置成true,才能获取控件的点击事件
        searchView.setIconified(false);//输入框内icon不显示
        searchView.requestFocusFromTouch();//模拟焦点点击事件

        searchView.setFocusable(false);//禁止弹出输入法，在某些情况下有需要
        searchView.clearFocus();//禁止弹出输入法，在某些情况下有需要

//事件监听
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                //SnackbarUtil.show(toolbar, query);
                return false;
            }
            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.l1:
                Intent intent  = new Intent(getActivity(), ResourceManager.class);
                startActivity(intent);
                break;
            case R.id.l2:
                break;

            case R.id.l3:
                break;

            case R.id.l4:
                break;

            default: break;
        }
    }


}
